package com.ytjj.qmyx.admin.controller.supplyChain;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.annotation.oplog.annotation.OperationProductLog;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.SupplyChainProductRequest;
import com.ytjj.qmyx.admin.model.request.SupplyChainRequest;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * 供应链商品管理
 */
@RestController
@RequestMapping("/supplyChain-product")
@Slf4j
public class SupplyChainProductController {

    @Resource
    private SupplyApiConfig supplyApiConfig;

    /**
     * 供应链商品导入
     * @param file
     */
    @PostMapping("/excelImport")
    @OperationProductLog(optModule = SysLogConstants.SHELVES_PRODUCT, optType = SysLogConstants.OPERATION_UPDATE, desc = "供应链商品导入")
    public String excelImport(MultipartFile file) throws IOException {
        // 导入订单
        if (null == file){
            throw new ApiException("请上传文件");
        }
        Sheet sheet = new Sheet(1, 1, SupplyChainProductRequest.class);
        String fileName = file.getOriginalFilename();
        List<Object> list = new ArrayList<>();
        try {
            list = EasyExcelFactory.read(new BufferedInputStream(file.getInputStream()), sheet);
        } catch (Exception e) {
            throw new ApiException("请导入正确的格式");
        }
        if (list.size() == 0){
            throw new ApiException("请导入正确的格式");
        }
        List<SupplyChainProductRequest> collect = list.stream()
                .map(item -> (SupplyChainProductRequest) item)
                .collect(Collectors.toList());
//        if (collect.stream().filter(e -> null != e.getProductId()).map(SupplyChainProductRequest::getProductId).count() == 0){
//            throw new ApiException("商品id不能为空，请导入正确的格式");
//        }
        Integer adminId = UserUtil.getAdminId();
        SupplyChainRequest supplyChainRequest = new SupplyChainRequest();
        supplyChainRequest.setProductRequests(collect);
        supplyChainRequest.setFileName(fileName);
        supplyChainRequest.setAdminId(adminId);
        String s = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/product/excelImport",
                JSONObject.toJSONString(supplyChainRequest), supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 获取导入记录
     * @return
     */
    @GetMapping("/getImportMsg")
    public String getImportMsg(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum.toString());
        map.put("pageSize",pageSize.toString());
        return HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/product/getImportMsg", map, supplyApiConfig.getHeadsMap());
    }
    /**
     * 获取详情导入记录
     * @return
     */
    @GetMapping("/getImportInfoMsg")
    public String getImportInfoMsg(@RequestParam Integer id,
                                   @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        Map<String, String> map = new HashMap<>();
        map.put("id",id.toString());
        map.put("pageNum",pageNum.toString());
        map.put("pageSize",pageSize.toString());
        return HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/product/getImportInfoMsg", map, supplyApiConfig.getHeadsMap());
    }
}
